<?php	
    require_once('paths.inc');
    require_once('security.inc');

    require_once('page.inc');    
    require_once('utils.inc');
    require_once(DB_PATH.'db.inc');
    require_once(INCLUDE_PATH.'constants.inc');    
    
	session_start();
    //check_permissions(CT_PERM_ADMIN_ABONAMENT);
	ob_start();
	
	$db = db_connect();
	$id_factura = $_GET['id'];

	select_language();

	$id_client = $_SESSION['user_logat']->id_client;
	
	try {
        $factura = FacturaClient::queryLoadFactura($db, $id_client, $id_factura);
        $user_admin = User::queryLoad($db, $_SESSION['client_logat']->admin_user_id);
    }
    catch (QueryLoadFacturaException $e) {
        redirectOnError(CT_PRINT_FACTURA_CLIENT_FAIL);
    }
    catch (QueryLoadUserException $e) {
        redirectOnError(CT_PRINT_FACTURA_CLIENT_FAIL);
    }
    
	header("Pragma: public"); // required                                                                                                                        
    header("Expires: 0");                                                                                                                                    
	header("Content-Description: File Transfer");                                                                                                        
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");                                                                             
	header("Content-type: application/pdf");                                                                                                                 
	header('Content-Transfer-Encoding: binary');                       
	$cale_fisier = USERS_PATH . $_SESSION['user_logat']->id_user . "/factura_client_" . $id_factura . ".pdf";                                                                                      
	header("Content-Disposition: attachment; filename=\"" . basename($cale_fisier) . '"');  
	                                                     
    print_factura_clientWithOO($db, $_SESSION['client_logat'], $factura, $user_admin->nume.' '.$user_admin->prenume);

    readfile($cale_fisier);                                             
	exit();        
?>
